package com.goodwill.androidtest.tree;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.goodwill.androidtest.R;
import com.goodwill.androidtest.tree.adapter.MyTreeListViewAdapter;
import com.goodwill.androidtest.tree.adapter.TreeListViewAdapter;
import com.goodwill.androidtest.tree.bean.MyNodeBean;
import com.goodwill.androidtest.tree.bean.Node;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TreeActivity extends AppCompatActivity {

    private ListView treeLv;
    private Button checkSwitchBtn;
    private MyTreeListViewAdapter<MyNodeBean> adapter;
    private List<MyNodeBean> mDatas = new ArrayList<MyNodeBean>();
    //标记是显示Checkbox还是隐藏
    private boolean isHide = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tree);
        initDatas();
        treeLv = (ListView) this.findViewById(R.id.tree_lv);
        checkSwitchBtn = (Button) this.findViewById(R.id.check_switch_btn);

        checkSwitchBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (isHide) {
                    isHide = false;
                } else {
                    isHide = true;
                }
                //切换checkbox
                adapter.updateView(isHide);
            }

        });
        try {
            adapter = new MyTreeListViewAdapter<MyNodeBean>(treeLv, this,
                    mDatas, 10, isHide);

            adapter.setOnTreeNodeClickListener(new TreeListViewAdapter.OnTreeNodeClickListener() {
                @Override
                public void onClick(Node node, int position) {
                    if (node.isLeaf()) {
                        Toast.makeText(getApplicationContext(), node.getName(),
                                Toast.LENGTH_SHORT).show();
                    } else {
                        List<MyNodeBean> childNodes = getChildNodes(node);
                        mDatas.addAll(childNodes);
                        adapter.notifyDataSetChanged();
                    }
                }

                @Override
                public void onCheckChange(Node node, int position,
                                          List<Node> checkedNodes) {
                    // TODO Auto-generated method stub

                    StringBuffer sb = new StringBuffer();
                    for (Node n : checkedNodes) {
                        int pos = n.getId() - 1;
                        sb.append(mDatas.get(pos).getName()).append("---")
                                .append(pos + 1).append(";");

                    }

                    Toast.makeText(getApplicationContext(), sb.toString(),
                            Toast.LENGTH_SHORT).show();
                }

            });
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        treeLv.setAdapter(adapter);

    }

    private List<MyNodeBean> getChildNodes(Node pNode) {
        List<MyNodeBean> childNodes = new ArrayList<>();
        for (MyNodeBean node : mList) {
            if (node.getPid() == pNode.getId()) {
                childNodes.add(node);
            }
        }

        return childNodes;
    }

    private static List<MyNodeBean> mList = new ArrayList<>();

    static {
        mList.add(new MyNodeBean(1, 0, "中国古代"));
        mList.add(new MyNodeBean(2, 1, "唐朝"));
        mList.add(new MyNodeBean(3, 1, "宋朝"));
        mList.add(new MyNodeBean(4, 1, "明朝"));
        mList.add(new MyNodeBean(5, 2, "李世民"));
        mList.add(new MyNodeBean(6, 2, "李白"));

        mList.add(new MyNodeBean(7, 3, "赵匡胤"));
        mList.add(new MyNodeBean(8, 3, "苏轼"));

        mList.add(new MyNodeBean(9, 4, "朱元璋"));
        mList.add(new MyNodeBean(10, 4, "唐伯虎"));
        mList.add(new MyNodeBean(11, 4, "文征明"));
        mList.add(new MyNodeBean(12, 7, "赵建立"));
        mList.add(new MyNodeBean(13, 8, "苏东东"));
        mList.add(new MyNodeBean(14, 10, "秋香"));
    }

    private void initDatas() {
        mDatas.add(new MyNodeBean(1, 0, "中国古代"));
//        mDatas.add(new MyNodeBean(2, 1, "唐朝"));
//        mDatas.add(new MyNodeBean(3, 1, "宋朝"));
//        mDatas.add(new MyNodeBean(4, 1, "明朝"));
//        mDatas.add(new MyNodeBean(5, 2, "李世民"));
//        mDatas.add(new MyNodeBean(6, 2, "李白"));
//
//        mDatas.add(new MyNodeBean(7, 3, "赵匡胤"));
//        mDatas.add(new MyNodeBean(8, 3, "苏轼"));
//
//        mDatas.add(new MyNodeBean(9, 4, "朱元璋"));
//        mDatas.add(new MyNodeBean(10, 4, "唐伯虎"));
//        mDatas.add(new MyNodeBean(11, 4, "文征明"));
//        mDatas.add(new MyNodeBean(12, 7, "赵建立"));
//        mDatas.add(new MyNodeBean(13, 8, "苏东东"));
//        mDatas.add(new MyNodeBean(14, 10, "秋香"));
    }
}
